<?php

namespace common\models\xcx;

use addons\Wechat\common\models\Fans;
use addons\Wechat\common\models\FansTagMap;
use common\helpers\RegularHelper;
use common\helpers\StringHelper;
use common\models\member\Member;
use common\models\project\ProjectKf;
use Yii;
use yii\behaviors\BlameableBehavior;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "hxdc_xcx_kf".
 *
 * @property int $id
 * @property int $member_id 用户id
 * @property int $category 1顾问;2经理;3拓客
 * @property int $nickname 昵称
 * @property int $head_portrait 头像
 * @property int $mobile 手机号码
 * @property string $onewords 顾问一句话描述
 * @property string $loupan 楼盘
 * @property string $codeimg 小程序二维码
 * @property string $mycard 名片
 * @property string $signature 顾问签名
 * @property string $back_img 小程序背景图
 * @property string $wxCode 微信号
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $status 	审核状态[0:待审核;1已审核]
 */
class XcxKf extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'hxdc_xcx_kf';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['member_id', 'nickname', 'category','mobile'], 'required'],
            [['member_id', 'category', 'created_at', 'updated_at', 'status'], 'integer'],
            [['onewords', 'codeimg', 'mycard', 'back_img'], 'string', 'max' => 255],
            [['loupan'], 'string', 'max' => 30],
            [['signature'], 'string', 'max' => 511],
            [['nickname'], 'string', 'max' => 50],
            [['head_portrait'], 'string', 'max' => 255],
            [['mobile'], 'string', 'max' => 20],
            [['wxCode'], 'string', 'max' => 63],
            ['mobile', 'match', 'pattern' => RegularHelper::mobile(),'message' => '不是一个有效的手机号码'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'member_id' => '用户 ID',
            'category' => '等级',
            'nickname' => '顾问姓名',
            'head_portrait' => '头像',
            'mobile' => '手机号码',
            'onewords' => '签名',
            'loupan' => '楼盘',
            'codeimg' => '小程序码',
            'mycard' => 'Mycard',
            'signature' => 'Signature',
            'back_img' => 'Back Img',
            'wxCode' => '微信号',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'status' => '审核状态',
        ];
    }
    /**
     * 关联会员
     */
    public function getMember()
    {
        return $this->hasOne(Member::class, ['id' => 'member_id']);
    }

    /**
     * 微信粉丝关联
     *
     * @return \yii\db\ActiveQuery
     */
    public function getFans()
    {
        return $this->hasMany(Fans::class,['member_id' => 'member_id']);
    }

    /**
     * 项目、顾问关联表关联
     *
     * @return \yii\db\ActiveQuery
     */
    public function getProjectkf()
    {
        return $this->hasMany(ProjectKf::class,['kf_id' => 'id']);
    }


    /**
     * @param bool $insert
     * @return bool
     * @throws \yii\base\Exception
     */
    public function beforeSave($insert)
    {
        if ($this->isNewRecord) {
            $this->created_at = time();
        }

        return parent::beforeSave($insert);
    }
    /**
     * @return array
     */
    public function behaviors()
    {
        $merchant_id = Yii::$app->services->merchant->getId();
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
            [
                'class' => BlameableBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['merchant_id'],
                ],
                'value' => !empty($merchant_id) ? $merchant_id : 0,
            ]
        ];
    }
}
